package com.wanlian.router.ui.integral;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.smart.android.image.ImageLoader;
import com.smart.android.ui.app.IntentExtra;
import com.smart.android.ui.bean.PageInfo;
import com.smart.android.ui.tools.BaseListFragment;
import com.smart.android.ui.tools.MyNiuBAdapter;
import com.wanlian.router.R;
import com.wanlian.router.bean.RankUser;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;

import static android.util.TypedValue.COMPLEX_UNIT_SP;

/**
 * Created by Hyu on 2018/8/30.
 * Email: fvaryu@163.com
 */
public class IntegralRankFragment extends BaseListFragment {
    public static final String WEEK = "week";
    public static final String MONTH = "month";
    public static final String ALL = "";

    private RankAdapter mRankAdapter;
    private List<RankUser> mIntegralRankUsers;


    public static IntegralRankFragment newInstance(String type) {

        Bundle args = new Bundle();
        args.putString(IntentExtra.EXTRA_OBJ, type);
        IntegralRankFragment fragment = new IntegralRankFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        mIntegralRankUsers = new ArrayList<>();
        setAdapter(mRankAdapter = new RankAdapter(getActivity(), mIntegralRankUsers));
        disableLoadMore();
    }

    @Override
    protected void initData(View view) {
        super.initData(view);
    }


    @Override
    protected void loadData(final boolean isRefresh) {
        super.loadData(isRefresh);
        if (!canToLoad(isRefresh)) {
            return;
        }

        PageInfo info = new PageInfo(1, 10);
        NetWorkingClient.getInstance().queryIntegralRank(getActivity(), info, getArguments().getString(IntentExtra.EXTRA_OBJ), new INetCallBack<List<RankUser>>() {
            @Override
            public void onFinish(ResponseData response, @Nullable List<RankUser> rankUsers) {
                resetPull();
                if (response.isSuccess()) {
                    if (isRefresh) {
                        mIntegralRankUsers.clear();
                    }

                    if (rankUsers != null) {
                        mIntegralRankUsers.addAll(rankUsers);
                    }

                    mRankAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    class RankAdapter extends MyNiuBAdapter<RankUser> {

        public RankAdapter(Context context, List<RankUser> mTs) {
            super(context, mTs);
        }

        @Override
        public int layout() {
            return R.layout.layout_item_integral_rank;
        }

        @Override
        public MyViewHolder<RankUser> holder(View view) {
            return new RankHolder(view);
        }
    }

    class RankHolder extends MyNiuBAdapter.MyViewHolder<RankUser> {

        @BindView(R.id.tv_rank)
        TextView mRank;
        @BindView(R.id.tv_name)
        TextView mName;
        @BindView(R.id.tv_integral)
        TextView mIntegral;
        @BindView(R.id.image_avatar)
        ImageView mAvatar;

        public RankHolder(View view) {
            super(view);
        }

        @Override
        public void setData(int i, RankUser rankUser) {
            ImageLoader.loadCircle(mAvatar.getContext(), rankUser.getHeadimg(), mAvatar);

            mName.setText(rankUser.getUsername());
            mIntegral.setText(String.format(Locale.getDefault(), "%.6f", rankUser.getScore()));

            mRank.setText(String.valueOf(i + 1));
            mRank.setTextColor(getColorById(R.color.white));
            switch (i) {
                case 0: {
                    mRank.setBackgroundResource(R.drawable.ic_rank_first);
                }
                break;
                case 1: {
                    mRank.setBackgroundResource(R.drawable.ic_rank_second);
                }
                break;
                case 2: {
                    mRank.setBackgroundResource(R.drawable.ic_rank_third);
                }
                break;
                default: {
                    mRank.setTextColor(getColorById(R.color.color_66));
                    mRank.setBackgroundResource(0);
                }
                break;
            }

            if (i > 2) {
                mRank.setTextSize(COMPLEX_UNIT_SP,14);
            }else {
                mRank.setTextSize(COMPLEX_UNIT_SP,10);
            }
        }
    }
}
